package options;

import gui.OptionsTest;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.mail.MessagingException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import mail.MyModelEmail;

/**
 * Optionsfenster der Mails
 * 
 * @author Michi
 * 
 */
public class MailOptionPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -122399074596572287L;
	private String[] windowColorsSeperated;
	private JTextField mailTextField;
	private JTextField imapServerTextField;
	private JTextField pop3ServerTextField;
	private JTextField smtpServerTextField;
	private JTextField imapPortTextField;
	private JTextField pop3PortTextField;
	private JPasswordField passwordField;
	private String pw;
	private JComboBox comboBox;

	// Sprache
	int speak = 0;

	String[] ungelesen = { "E-Mails ungelesen", "Unread emails",
			"Inlectus Mail" };
	String[] ungelesenSetzen = { "E-Mails auf Server auf \"ungelesen\"",
			"Emails on server\"unread\"", "A serverus mails \"nondum\"" };
	String[] loeschenVonS = { "E-Mails vom Server l\u00F6schen",
			"Delete email from server", "Delere email moderatri" };

	String[] alleLoeschen = { "Alle E-Mails auf Server l\u00F6schen",
			"Delere all emails on server", "Delere omnes sodales in server" };

	String[] loeschen = { "E-Mails L\u00F6schen", "Deleting Emails",
			"Delere emails" };
	String[] verbEins = { "Verbindungseinstellungen", "Connection settings",
			"Iunctio occasus" };
	String[] mailadresse = { "Emailadresse", "Email address",
			"Email inscriptio" };

	String[] mailpw = { "E-Mail Passwort", "Email Password", "Signum Email" };
	String[] zuruecksetzen = { "Zur\u00FCcksetzen", "Reset", "Sustineo" };
	String[] lokalloeschen = { "Lokale E-Mails l\u00F6schen",
			"Local delete emails", "Local Delete emails" };
	private JTextField smtpPortTextField;

	/**
	 * Create the panel.
	 */
	public MailOptionPanel(final OptionsTest optionsTestInstance) {
		speak = Integer.parseInt(optionsTestInstance.getOr().getLanguage());

		String windowColors = optionsTestInstance.getOr().getColorSetting();
		windowColorsSeperated = windowColors.split(",");
		setBackground(new Color(Integer.parseInt(windowColorsSeperated[0]),
				Integer.parseInt(windowColorsSeperated[1]),
				Integer.parseInt(windowColorsSeperated[2])));
		setMinimumSize(new Dimension(600, 700));
		setMaximumSize(new Dimension(600, 700));
		setPreferredSize(new Dimension(600, 700));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, Double.MIN_VALUE };
		setLayout(gridBagLayout);

		JLabel lblOption = new JLabel(verbEins[speak]);
		lblOption.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblOption = new GridBagConstraints();
		gbc_lblOption.anchor = GridBagConstraints.WEST;
		gbc_lblOption.insets = new Insets(0, 0, 5, 5);
		gbc_lblOption.gridx = 1;
		gbc_lblOption.gridy = 1;
		add(lblOption, gbc_lblOption);

		JLabel dummyLabel0 = new JLabel("   ");
		dummyLabel0.setPreferredSize(new Dimension(12, 25));
		GridBagConstraints gbc_dummyLabel0 = new GridBagConstraints();
		gbc_dummyLabel0.insets = new Insets(0, 0, 5, 5);
		gbc_dummyLabel0.gridx = 0;
		gbc_dummyLabel0.gridy = 2;
		add(dummyLabel0, gbc_dummyLabel0);

		JLabel lblEmailadresse = new JLabel(mailadresse[speak]);
		GridBagConstraints gbc_lblEmailadresse = new GridBagConstraints();
		gbc_lblEmailadresse.anchor = GridBagConstraints.EAST;
		gbc_lblEmailadresse.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmailadresse.gridx = 1;
		gbc_lblEmailadresse.gridy = 3;
		add(lblEmailadresse, gbc_lblEmailadresse);

		mailTextField = new JTextField();
		mailTextField.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				optionsTestInstance.setMailOpt1(mailTextField.getText() + ","
						+ imapServerTextField.getText() + ","
						+ pop3ServerTextField.getText() + ","
						+ smtpServerTextField.getText() + ","
						+ imapPortTextField.getText() + ","
						+ pop3PortTextField.getText() + ","
						+ comboBox.getSelectedItem() + "," + pw + ","
						+ smtpPortTextField.getText());
			}
		});
		GridBagConstraints gbc_mailTextField = new GridBagConstraints();
		gbc_mailTextField.insets = new Insets(0, 0, 5, 0);
		gbc_mailTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_mailTextField.gridx = 2;
		gbc_mailTextField.gridy = 3;
		add(mailTextField, gbc_mailTextField);
		mailTextField.setColumns(10);

		JLabel lblImapserver = new JLabel("Imap Server");
		GridBagConstraints gbc_lblImapserver = new GridBagConstraints();
		gbc_lblImapserver.anchor = GridBagConstraints.EAST;
		gbc_lblImapserver.insets = new Insets(0, 0, 5, 5);
		gbc_lblImapserver.gridx = 1;
		gbc_lblImapserver.gridy = 4;
		add(lblImapserver, gbc_lblImapserver);

		imapServerTextField = new JTextField();
		imapServerTextField.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				optionsTestInstance.setMailOpt1(mailTextField.getText() + ","
						+ imapServerTextField.getText() + ","
						+ pop3ServerTextField.getText() + ","
						+ smtpServerTextField.getText() + ","
						+ imapPortTextField.getText() + ","
						+ pop3PortTextField.getText() + ","
						+ comboBox.getSelectedItem() + "," + pw + ","
						+ smtpPortTextField.getText());
			}
		});
		GridBagConstraints gbc_imapServerTextField = new GridBagConstraints();
		gbc_imapServerTextField.insets = new Insets(0, 0, 5, 0);
		gbc_imapServerTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_imapServerTextField.gridx = 2;
		gbc_imapServerTextField.gridy = 4;
		add(imapServerTextField, gbc_imapServerTextField);
		imapServerTextField.setColumns(10);

		JLabel lblSmtpserver = new JLabel("Pop3 Server");
		GridBagConstraints gbc_lblSmtpserver = new GridBagConstraints();
		gbc_lblSmtpserver.anchor = GridBagConstraints.EAST;
		gbc_lblSmtpserver.insets = new Insets(0, 0, 5, 5);
		gbc_lblSmtpserver.gridx = 1;
		gbc_lblSmtpserver.gridy = 5;
		add(lblSmtpserver, gbc_lblSmtpserver);

		pop3ServerTextField = new JTextField();
		pop3ServerTextField.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				optionsTestInstance.setMailOpt1(mailTextField.getText() + ","
						+ imapServerTextField.getText() + ","
						+ pop3ServerTextField.getText() + ","
						+ smtpServerTextField.getText() + ","
						+ imapPortTextField.getText() + ","
						+ pop3PortTextField.getText() + ","
						+ comboBox.getSelectedItem() + "," + pw + ","
						+ smtpPortTextField.getText());
			}
		});
		GridBagConstraints gbc_pop3ServerTextField = new GridBagConstraints();
		gbc_pop3ServerTextField.insets = new Insets(0, 0, 5, 0);
		gbc_pop3ServerTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_pop3ServerTextField.gridx = 2;
		gbc_pop3ServerTextField.gridy = 5;
		add(pop3ServerTextField, gbc_pop3ServerTextField);
		pop3ServerTextField.setColumns(10);

		JLabel lblNewLabel = new JLabel("Smtp Server");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 1;
		gbc_lblNewLabel.gridy = 6;
		add(lblNewLabel, gbc_lblNewLabel);

		smtpServerTextField = new JTextField();
		smtpServerTextField.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				optionsTestInstance.setMailOpt1(mailTextField.getText() + ","
						+ imapServerTextField.getText() + ","
						+ pop3ServerTextField.getText() + ","
						+ smtpServerTextField.getText() + ","
						+ imapPortTextField.getText() + ","
						+ pop3PortTextField.getText() + ","
						+ comboBox.getSelectedItem() + "," + pw + ","
						+ smtpPortTextField.getText());
			}
		});
		GridBagConstraints gbc_smtpServerTextField = new GridBagConstraints();
		gbc_smtpServerTextField.insets = new Insets(0, 0, 5, 0);
		gbc_smtpServerTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_smtpServerTextField.gridx = 2;
		gbc_smtpServerTextField.gridy = 6;
		add(smtpServerTextField, gbc_smtpServerTextField);
		smtpServerTextField.setColumns(10);

		JLabel lblImapPort = new JLabel("Imap Port");
		GridBagConstraints gbc_lblImapPort = new GridBagConstraints();
		gbc_lblImapPort.anchor = GridBagConstraints.EAST;
		gbc_lblImapPort.insets = new Insets(0, 0, 5, 5);
		gbc_lblImapPort.gridx = 1;
		gbc_lblImapPort.gridy = 7;
		add(lblImapPort, gbc_lblImapPort);

		imapPortTextField = new JTextField();
		imapPortTextField.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				optionsTestInstance.setMailOpt1(mailTextField.getText() + ","
						+ imapServerTextField.getText() + ","
						+ pop3ServerTextField.getText() + ","
						+ smtpServerTextField.getText() + ","
						+ imapPortTextField.getText() + ","
						+ pop3PortTextField.getText() + ","
						+ comboBox.getSelectedItem() + "," + pw + ","
						+ smtpPortTextField.getText());
			}
		});
		GridBagConstraints gbc_imapPortTextField = new GridBagConstraints();
		gbc_imapPortTextField.insets = new Insets(0, 0, 5, 0);
		gbc_imapPortTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_imapPortTextField.gridx = 2;
		gbc_imapPortTextField.gridy = 7;
		add(imapPortTextField, gbc_imapPortTextField);
		imapPortTextField.setColumns(10);

		JLabel lblPopPort = new JLabel("Pop3 Port");
		GridBagConstraints gbc_lblPopPort = new GridBagConstraints();
		gbc_lblPopPort.anchor = GridBagConstraints.EAST;
		gbc_lblPopPort.insets = new Insets(0, 0, 5, 5);
		gbc_lblPopPort.gridx = 1;
		gbc_lblPopPort.gridy = 8;
		add(lblPopPort, gbc_lblPopPort);

		pop3PortTextField = new JTextField();
		pop3PortTextField.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				optionsTestInstance.setMailOpt1(mailTextField.getText() + ","
						+ imapServerTextField.getText() + ","
						+ pop3ServerTextField.getText() + ","
						+ smtpServerTextField.getText() + ","
						+ imapPortTextField.getText() + ","
						+ pop3PortTextField.getText() + ","
						+ comboBox.getSelectedItem() + "," + pw + ","
						+ smtpPortTextField.getText());
			}
		});
		GridBagConstraints gbc_pop3PortTextField = new GridBagConstraints();
		gbc_pop3PortTextField.insets = new Insets(0, 0, 5, 0);
		gbc_pop3PortTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_pop3PortTextField.gridx = 2;
		gbc_pop3PortTextField.gridy = 8;
		add(pop3PortTextField, gbc_pop3PortTextField);
		pop3PortTextField.setColumns(10);

		JLabel lblSmtpPort = new JLabel("Smtp Port");
		GridBagConstraints gbc_lblSmtpPort = new GridBagConstraints();
		gbc_lblSmtpPort.anchor = GridBagConstraints.EAST;
		gbc_lblSmtpPort.insets = new Insets(0, 0, 5, 5);
		gbc_lblSmtpPort.gridx = 1;
		gbc_lblSmtpPort.gridy = 9;
		add(lblSmtpPort, gbc_lblSmtpPort);

		smtpPortTextField = new JTextField();
		smtpPortTextField.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {

				optionsTestInstance.setMailOpt1(mailTextField.getText() + ","
						+ imapServerTextField.getText() + ","
						+ pop3ServerTextField.getText() + ","
						+ smtpServerTextField.getText() + ","
						+ imapPortTextField.getText() + ","
						+ pop3PortTextField.getText() + ","
						+ comboBox.getSelectedItem() + "," + pw + ","
						+ smtpPortTextField.getText());

			}
		});
		GridBagConstraints gbc_smtpPortTextField = new GridBagConstraints();
		gbc_smtpPortTextField.insets = new Insets(0, 0, 5, 0);
		gbc_smtpPortTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_smtpPortTextField.gridx = 2;
		gbc_smtpPortTextField.gridy = 9;
		add(smtpPortTextField, gbc_smtpPortTextField);
		smtpPortTextField.setColumns(10);

		JLabel lblProvider = new JLabel("Provider");
		GridBagConstraints gbc_lblProvider = new GridBagConstraints();
		gbc_lblProvider.anchor = GridBagConstraints.EAST;
		gbc_lblProvider.insets = new Insets(0, 0, 5, 5);
		gbc_lblProvider.gridx = 1;
		gbc_lblProvider.gridy = 10;
		add(lblProvider, gbc_lblProvider);

		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				optionsTestInstance.setMailOpt1(mailTextField.getText() + ","
						+ imapServerTextField.getText() + ","
						+ pop3ServerTextField.getText() + ","
						+ smtpServerTextField.getText() + ","
						+ imapPortTextField.getText() + ","
						+ pop3PortTextField.getText() + ","
						+ comboBox.getSelectedItem() + "," + pw + ","
						+ smtpPortTextField.getText());
			}
		});
		comboBox.addItem("pop3");
		comboBox.addItem("imap");
		comboBox.addItem("imaps");
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 2;
		gbc_comboBox.gridy = 10;
		add(comboBox, gbc_comboBox);

		JLabel lblEmailpasswort = new JLabel(mailpw[speak]);
		GridBagConstraints gbc_lblEmailpasswort = new GridBagConstraints();
		gbc_lblEmailpasswort.anchor = GridBagConstraints.EAST;
		gbc_lblEmailpasswort.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmailpasswort.gridx = 1;
		gbc_lblEmailpasswort.gridy = 11;
		add(lblEmailpasswort, gbc_lblEmailpasswort);

		passwordField = new JPasswordField();
		passwordField.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				char[] ca = passwordField.getPassword();
				pw = "";
				for (int i = 0; i < ca.length; i++) {
					pw = pw + ca[i];
				}

				optionsTestInstance.setMailOpt1(mailTextField.getText() + ","
						+ imapServerTextField.getText() + ","
						+ pop3ServerTextField.getText() + ","
						+ smtpServerTextField.getText() + ","
						+ imapPortTextField.getText() + ","
						+ pop3PortTextField.getText() + ","
						+ comboBox.getSelectedItem() + "," + pw + ","
						+ smtpPortTextField.getText());

			}
		});
		GridBagConstraints gbc_passwordField = new GridBagConstraints();
		gbc_passwordField.insets = new Insets(0, 0, 5, 0);
		gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
		gbc_passwordField.gridx = 2;
		gbc_passwordField.gridy = 11;
		add(passwordField, gbc_passwordField);

		JLabel dummyLabel = new JLabel("   ");
		dummyLabel.setPreferredSize(new Dimension(12, 25));
		GridBagConstraints gbc_dummyLabel = new GridBagConstraints();
		gbc_dummyLabel.insets = new Insets(0, 0, 5, 5);
		gbc_dummyLabel.gridx = 1;
		gbc_dummyLabel.gridy = 13;
		add(dummyLabel, gbc_dummyLabel);

		JLabel lblZurcksetzen = new JLabel(zuruecksetzen[speak]);
		lblZurcksetzen.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		GridBagConstraints gbc_lblZurcksetzen = new GridBagConstraints();
		gbc_lblZurcksetzen.anchor = GridBagConstraints.WEST;
		gbc_lblZurcksetzen.insets = new Insets(0, 0, 5, 5);
		gbc_lblZurcksetzen.gridx = 1;
		gbc_lblZurcksetzen.gridy = 14;
		add(lblZurcksetzen, gbc_lblZurcksetzen);

		JLabel dummyLabel1 = new JLabel("   ");
		dummyLabel1.setPreferredSize(new Dimension(12, 25));
		GridBagConstraints gbc_dummyLabel1 = new GridBagConstraints();
		gbc_dummyLabel1.insets = new Insets(0, 0, 5, 5);
		gbc_dummyLabel1.gridx = 1;
		gbc_dummyLabel1.gridy = 15;
		add(dummyLabel1, gbc_dummyLabel1);

		JLabel lblEmailsAufServer = new JLabel(alleLoeschen[speak]);
		GridBagConstraints gbc_lblEmailsAufServer = new GridBagConstraints();
		gbc_lblEmailsAufServer.anchor = GridBagConstraints.EAST;
		gbc_lblEmailsAufServer.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmailsAufServer.gridx = 1;
		gbc_lblEmailsAufServer.gridy = 17;
		add(lblEmailsAufServer, gbc_lblEmailsAufServer);

		JButton btnEmailsVomServer = new JButton(loeschenVonS[speak]);
		btnEmailsVomServer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyModelEmail mme = new MyModelEmail();
				try {
					mme.deleteAllMails();
				} catch (MessagingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		GridBagConstraints gbc_btnEmailsVomServer = new GridBagConstraints();
		gbc_btnEmailsVomServer.insets = new Insets(0, 0, 5, 0);
		gbc_btnEmailsVomServer.gridx = 2;
		gbc_btnEmailsVomServer.gridy = 17;
		add(btnEmailsVomServer, gbc_btnEmailsVomServer);

		JLabel lblEmailsAufServer_1 = new JLabel(ungelesenSetzen[speak]);
		GridBagConstraints gbc_lblEmailsAufServer_1 = new GridBagConstraints();
		gbc_lblEmailsAufServer_1.insets = new Insets(0, 0, 0, 5);
		gbc_lblEmailsAufServer_1.gridx = 1;
		gbc_lblEmailsAufServer_1.gridy = 18;
		add(lblEmailsAufServer_1, gbc_lblEmailsAufServer_1);

		JButton btnEmailsUngelesen = new JButton(ungelesen[speak]);
		btnEmailsUngelesen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyModelEmail mme = new MyModelEmail();
				try {
					mme.setAllMailsUnread();
				} catch (MessagingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		btnEmailsUngelesen.setPreferredSize(new Dimension(211, 29));
		GridBagConstraints gbc_btnEmailsUngelesen = new GridBagConstraints();
		gbc_btnEmailsUngelesen.gridx = 2;
		gbc_btnEmailsUngelesen.gridy = 18;
		add(btnEmailsUngelesen, gbc_btnEmailsUngelesen);

		String emailSettings = optionsTestInstance.getOr().getMailOpt1();
		String[] mailSettings = emailSettings.split(",");
		mailTextField.setText(mailSettings[0]);
		imapServerTextField.setText(mailSettings[1]);
		pop3ServerTextField.setText(mailSettings[2]);
		smtpServerTextField.setText(mailSettings[3]);
		imapPortTextField.setText(mailSettings[4]);
		pop3PortTextField.setText(mailSettings[5]);
		if (mailSettings[6].equals("imap")) {
			comboBox.setSelectedIndex(1);
		} else if (mailSettings[6].equals("pop3")) {
			comboBox.setSelectedIndex(0);
		} else if (mailSettings[6].equals("imaps")) {
			comboBox.setSelectedIndex(2);
		} else {
			comboBox.setSelectedIndex(-1);
		}
		passwordField.setText(mailSettings[7]);
		smtpPortTextField.setText(mailSettings[8]);

	}
}
